import 'package:flutter/material.dart';
import 'package:xm_app/common/logger/LoggerUtil.dart';
import 'package:xm_app/common/ui/AppTheme.dart';
import 'package:xm_app/common/ui/ui_adapter.dart';

class HotTitleWidget extends StatefulWidget {
  final Function onRefreshPressed;
  final int lastQty;
  HotTitleWidget({Key key, this.onRefreshPressed, this.lastQty}) : super(key: key);
  @override
  _HotTitleWidgetState createState() => _HotTitleWidgetState();
}

class _HotTitleWidgetState extends State<HotTitleWidget> with SingleTickerProviderStateMixin {
  AnimationController _controller;
  Animation<double> _animation;

  @override
  void initState() {
    super.initState();
    logDebug("HotTitleWidget initState");
    _controller = AnimationController(duration: Duration(milliseconds: 500), vsync: this);
    _animation = Tween<double>(begin: 0, end: 2).animate(_controller);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: getHeight(50),
      child: Row(
        children: [
          // SizedBox(width: getWidth(5)),
          Icon(Icons.recommend),
          SizedBox(width: getWidth(5)),
          Text(
            "本周热播",
            style: TextStyle(fontSize: getSp(18), fontWeight: FontWeight.w700),
          ),
          SizedBox(width: getWidth(15)),
          Text(
            "今日更新",
            style: TextStyle(fontSize: getSp(12)),
          ),
          SizedBox(width: getWidth(2)),
          Container(
            height: getHeight(15),
            alignment: Alignment.center,
            padding: EdgeInsets.symmetric(
              horizontal: 5,
            ),
            constraints: BoxConstraints(
              minWidth: getWidth(30),
            ),
            child: Text(
              "${widget.lastQty ?? ""}",
              textAlign: TextAlign.center,
              style: TextStyle(fontSize: getSp(10), color: Colors.white),
            ),
            decoration: BoxDecoration(borderRadius: BorderRadius.circular(8), color: AppTheme.pinkColor),
          ),
          Spacer(),
          GestureDetector(
            onTap: () {
              if (_controller.isAnimating) return;
              if (_controller.isDismissed || _controller.isCompleted) {
                _controller?.reset();
                _controller?.forward();
                widget.onRefreshPressed?.call();
              }
            },
            child: Row(
              children: [
                RotationTransition(
                  turns: _animation,
                  child: Icon(
                    Icons.refresh,
                    color: AppTheme.pinkColor,
                    size: getSp(18),
                  ),
                ),
                Text("换一换", style: TextStyle(fontSize: getSp(12))),
              ],
            ),
          ),
          SizedBox(width: getWidth(10)),
        ],
      ),
    );
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }
}
